{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use Stripe with agents.json\n",
    "This notebook demonstrates how to use agents.json to interact with the Stripe API. This simple example shows loading the agents.json file and using it to execute Stripe tasks.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install agentsjson\n",
    "%pip install openai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup API keys and references to `agents.json` files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "STRIPE_API_KEY=\"<your-stripe-api-key>\"\n",
    "OPENAI_API_KEY=\"<your-openai-api-key>\" \n",
    "agents_json_url = \"https://raw.githubusercontent.com/wild-card-ai/agents-json/refs/heads/master/agents_json/stripe/agents.json\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the agents.json file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson.core.models import Flow\n",
    "from agentsjson.core.models.bundle import Bundle\n",
    "from agentsjson.core import load_agents_json\n",
    "\n",
    "# load the agents.json file\n",
    "data: Bundle = load_agents_json(agents_json_url)\n",
    "agentsjson = data.agentsJson"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define the Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"Create a new Stripe product for Tie Dye T-Shirts priced at $10, $15, and $30 for small, medium, and large sizes\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson import ToolFormat, get_tool_prompt\n",
    "\n",
    "# Format the flows data for the prompt\n",
    "flows_context = get_tool_prompt(agentsjson)\n",
    "\n",
    "# Create the system prompt\n",
    "system_prompt = f\"\"\"You are an AI assistant that helps users interact with the Stripe API.\n",
    "You have access to the following API flows:\n",
    "\n",
    "{flows_context}\n",
    "\n",
    "Analyze the user's request and use the appropriate API flows to accomplish the task.\n",
    "You must give your arguments for the tool call as Structued Outputs JSON with keys `parameters` and `requestBody`\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configure Auth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson.core.models.auth import AuthType, BearerAuthConfig\n",
    "auth = BearerAuthConfig(type=AuthType.BEARER, token=STRIPE_API_KEY)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run the agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from agentsjson import execute, get_tools\n",
    "client = OpenAI(api_key=OPENAI_API_KEY)\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=\"gpt-4o\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": system_prompt},\n",
    "        {\"role\": \"user\", \"content\": query}\n",
    "    ],\n",
    "    tools=get_tools(agentsjson, format=ToolFormat.OPENAI),\n",
    "    temperature=0\n",
    ")\n",
    "\n",
    "response = execute(agentsjson, response, format=ToolFormat.OPENAI, auth=auth)\n",
    "\n",
    "response"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "newenv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
